package com.tarena.lbs.basic.web.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.api.PicUpdateParam;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.event.output.LocationShopOutputSource;
import com.tarena.lbs.basic.web.repository.StoreRepository;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.basic.bo.StoreBO;
import com.tarena.lbs.pojo.basic.param.StoreParam;
import com.tarena.lbs.pojo.basic.param.UserLocationParam;
import com.tarena.lbs.pojo.basic.query.AreaStoreQuery;
import com.tarena.lbs.pojo.basic.query.StoreQuery;
import com.tarena.lbs.pojo.marketing.param.UserCouponsParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StoreService {
    @Autowired
    private LocationShopOutputSource locationShopOutputSource;
    @Autowired
    private StoreRepository storeRepository;

    public BasePageList<StoreBO> storeList(StoreQuery query) {
        return storeRepository.storeList(query);
    }

    public void saveStore(StoreParam storeParam) throws BusinessException, IOException {
        //1.根据businessId 验证 business是否合法(启用禁用) 是否存在(count(*))
        checkBusiness(storeParam.getBusinessId());
        //2.根据仓储层
        Integer stoteId = storeRepository.saveStore(storeParam);
        //3.绑定2个业务图片 店铺logo业务200 店铺图片图片业务 300
        bindPictures(storeParam,stoteId);
    }
    @Autowired
    private AttachApi attachApi;
    private void bindPictures(StoreParam storeParam, Integer stoteId) {
        List<PicUpdateParam> picUpdateParams = new ArrayList<>();
        //封装 storeParam 传进来的 店铺图片list pidId buzType=300 buzId=storeId
        List<String> storeImagesIds = storeParam.getStoreImagesIds();
        if (CollectionUtils.isNotEmpty(storeImagesIds)){
            picUpdateParams = storeImagesIds.stream().map(picId->{
                PicUpdateParam param = new PicUpdateParam();
                param.setBusinessType(300);
                param.setBusinessId(stoteId);
                param.setId(Integer.valueOf(picId));
                return param;
            }).collect(Collectors.toList());
        }
        //storeParam 提交店铺的入参中 拿到logo图片id
        Integer logoId = Integer.valueOf(storeParam.getStoreLogo());
        PicUpdateParam logoParam = new PicUpdateParam();
        logoParam.setId(logoId);
        logoParam.setBusinessType(200);
        logoParam.setBusinessId(stoteId);
        picUpdateParams.add(logoParam);

        attachApi.batchUpdateBusiness(picUpdateParams);
    }

    @Autowired
    private BusinessService businessService;
    private void checkBusiness(Integer businessId) throws BusinessException {
        //判断存在不存在
        boolean exits = businessService.exitsBusiness(businessId);
        if (!exits){
            throw new BusinessException("105","商家根本不存在,无法新增店铺");
        }

    }

    public BasePageList<StoreBO> getCitiesStores(AreaStoreQuery query) throws BusinessException {
        //1.获取解析的用户
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        if (userPrinciple == null) {
            throw new BusinessException("101","用户解析认证失败");
        }
        //2.查询满足 当前登录用户所属商家范围 提交参数的区域范围内的店铺列表
        BasePageList<StoreBO> bos = storeRepository.getStoresByAreasAndBusiness(userPrinciple.getId(),query);
        return bos;
    }

    public StoreBO locationReport(UserLocationParam param) throws BusinessException {
        //1.获取用户信息
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        Asserts.isTrue(userPrinciple==null,new BusinessException("101","用户认证解析失败"));
        //2.调用仓储层 给我查询5公里内的某一个店铺信息 有可能查不到
        StoreBO bo=storeRepository.getStoreByLocationRadius(param,50D);

        if (bo!=null){
            //消息体数据
            UserCouponsParam userCouponsParam = new UserCouponsParam();
            userCouponsParam.setUserId(userPrinciple.getId());
            userCouponsParam.setShopId(bo.getId());
            Message<UserCouponsParam> message = MessageBuilder.withPayload(userCouponsParam).build();
            //发送一个消息 消费者调用领取优惠券的逻辑
            MessageChannel messageChannel = locationShopOutputSource.locationShopOutput();
            messageChannel.send(message);
        }
        return bo;
    }
}
